package com.example.animaltiontext_1;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mButton;
    private ImageView mImage;
    private TextView mText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        mButton = findViewById(R.id.button);
        mImage = findViewById(R.id.image);
        mText = findViewById(R.id.text);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
//            initAnimator();
//            initAnimatorJava();
//            initAnimator2();
//            initAnimator3();
//            initOBJectAnimator();
//            initObjectAnimator2();
            initObjectAnim2();
        }

    }

    private void initAnimator() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator);
        valueAnimator.setTarget(mImage);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("test123", "---------------------------" + value);
                //mImage.setRotation(value * 360);
                //mImage.setRotationX(value * 360);
                //mImage.setRotationY(value * 360);
                //mImage.setAlpha(1 - value);
                //mImage.getLayoutParams().width = (int) (mImage.getLayoutParams().width * value);
                //mImage.requestLayout();
                mImage.setTranslationX(value * 100);
                mImage.setTranslationY(value * 300);
                mText.setText(value + "");
            }
        });
        valueAnimator.start();
    }

    private  void initAnimatorJava(){
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1,0,1,0,1);
        valueAnimator.setDuration(4000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value= (float) animation.getAnimatedValue();
                Log.e("test123", "onAnimationUpdate: "+value);
//                mImage.setScaleX(value);
//                mImage.setScaleY(value);
                mImage.setAlpha(value);

            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }
    private void initAnimator2(){
        ValueAnimator valueAnimator = ValueAnimator.ofArgb(
          0xffff0000,0xff00ff00,0xff0000ff,0xffffffff,0xff000000
        );
        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int value= (int) animation.getAnimatedValue();
                mText.setBackgroundColor(value);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }
    private void initAnimator3(){
        Man boy = new Man("小王",18);
        Man old = new Man("老王",70);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                Man boy = (Man) startValue;
                Man old= (Man) endValue;
                int age= (int) (boy.getAge()+(old.getAge()- boy.getAge())*fraction);
                String name;
                if (age<30){
                    name="小王";
                }else if(age<60){
                    name="王哥";
                }else{
                    name="old王";
                }
                Man man=new Man(name,age);
                return man;
            }
        },boy,old);
        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                Man man= (Man) animation.getAnimatedValue();
                mText.setText(man.toString());
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }
    private void initOBJectAnimator(){
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mImage, "alpha",1,0,1,0);
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mImage, "Rotation",360,180,270,90,360);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                mImage, "translationY",0,300,-270,190,-360,0);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(2500);
        objectAnimator.start();
    }
    private  void  initObjectAnimator2(){
        Man man = new Man("lisan",1);
        ObjectAnimator animator = ObjectAnimator.ofInt(man, "age",1,100);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(4000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                mText.setText(man.toString());
                Log.d("TAG", man.toString());
            }
        });
        animator.start();
    }
    private void initObjectAnim2(){
        PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat(
                "Rotation",0,360,0,-360,0
        );
        PropertyValuesHolder colorHolder = PropertyValuesHolder.ofInt(
                "BackgroundColor",0xffff0000,0xff00ff00,0xff000000,0xff0000ff
        );
        PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofFloat(
                "ScaleX",1,0.4f,0.5f,1.2f,0.3f,1.0f
        );
        PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofFloat(
                "ScaleY",1,2,0.5f,1.6f,0.4f,1.0f
        );
        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat(
                "Alpha",1,0,1,0,0.7f,0.2f,1
        );
        PropertyValuesHolder translationYHolder = PropertyValuesHolder.ofFloat(
                "TranslationY",0,300,0,-300,0,300,0
        );




        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(
                mText,translationYHolder,rotationHolder,scaleXHolder,scaleYHolder,alphaHolder,colorHolder
        );

        objectAnimator.setDuration(2000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();

    }
}